/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tools;

import graphics.ProgressFrame;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mattia
 */
public class TimeManager {

    public static String getTimeToString() {
        return new SimpleDateFormat("dd-MM-yyyy HH:mm").format(new Date(System.currentTimeMillis()));
    }

    public static String getIntervalToString(long timeStartForLog) {
        return convertToInterval(timeStartForLog);
    }
    
    private boolean stop = false;
    private long timeStart;
    private ProgressFrame pr;
    public TimeManager(ProgressFrame pr) {
        this.pr = pr;
    }
    
    private static String convertToInterval(long start) {
        long time = System.currentTimeMillis()-start;
        int cen = (int) ((time / (10)  )        % 100);
        int sec = (int) ((time / (1000))        % 60 );
        int min = (int) ((time / (1000)) / (60) % 60 );
        return (min<10?"0"+min:min)+"m "+(sec<10?"0"+sec:sec)+"s "+(cen<10?"0"+cen:cen);
    }
    
    public String timeFromStart() {
        return convertToInterval(timeStart);
    }
    
    public void start() {
        if(pr != null) {
            this.timeStart = System.currentTimeMillis();
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    while(!stop) {
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(TimeManager.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        pr.Time.setText(timeFromStart());
                        //System.out.println("-- TIME --: "+min+"m "+sec+"s "+(cen==0?"00":cen));
                    }
                }
            });
            t.setName("Timer");
            t.start();
        }
    }
    public void stop() {
        this.stop = true;
    }
}
